package com.arrogames.blondbattle.game.rendering 
{
	import ash.tools.ListIteratingSystem;
	import com.arrogames.blondbattle.game.graphics.animation.Animatable;
	import com.arrogames.blondbattle.game.rendering.AnimationNode;
	import com.arrogames.blondbattle.game.movement.Spawner;
	import com.arrogames.blondbattle.game.combat.Faction;
	import flash.geom.Point;
	
	import org.as3commons.logging.api.ILogger;
	import org.as3commons.logging.api.getLogger;
	
	
	/**
	 * ...
	 * @author Arnoud van Steenwijk
	 */
	public class AnimationSystem extends ListIteratingSystem 
	{
		
		private static const logger:ILogger = getLogger(AnimationSystem);
		
		
		public function AnimationSystem() 
		{
			super(AnimationNode, updateNode);
		}
		
		private function updateNode(node:AnimationNode, time:Number) : void 
		{
			if (node.display.displayObject is Animatable) {
				var animatable:Animatable = node.display.displayObject as Animatable;
				var originalSpawn:String = animatable.getOriginalSpawn();
				var nodeVelocity:Point = node.motion.velocity;
				if (originalSpawn == Faction.HUMAN && nodeVelocity.x < 0) {
					logger.info('changing direction to West');
					animatable.changeDirection(Faction.HUMAN);
				} else if (originalSpawn == Faction.COMPUTER && nodeVelocity.x > 0) {
					logger.info('changing direction to East');
					animatable.changeDirection(Faction.COMPUTER);
				} 
				if ( nodeVelocity.x == 0 && animatable.isAnimating()) {
					animatable.stopAnimation();
				} else if ( !animatable.isAnimating() ) {
					animatable.startAnimation();
				}
			}
		}
		
	}

}